package com.zch.house.service.impl;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zch.house.dao.UsersDao;
import com.zch.house.entity.Users;
import com.zch.house.service.UsersService;
import com.zch.house.util.XmlSession;

public class UsersServiceImpl extends XmlSession implements UsersService {
	private final int PAGE_SIZE = 20;

	@Override
	public Map<String, Object> login(String name, String password) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		UsersDao dao = session.getMapper(UsersDao.class);
		List<Users> list = dao.getUsersByName(name);
		//提交事务并关闭session
		closeSession();
		Map<String, Object> map = new HashMap<>();
		if(list!=null){
			//用户名存在
			for(Users user:list){
				if(user.getU_pass().equals(password)){
					map.put("isLogin", true);//登陆成功
					map.put("user", user);//存入该用户信息
					break;
				}
			}
			//密码错误
			map.put("isLogin", false);
			map.put("errorMsg", "密码错误！");
		}else{
			//用户名不存在
			map.put("isLogin", false);
			map.put("errorMsg", "用户名不存在!");
		}
		return map;
	}

	@Override
	public List<Users> getAllUsersByPage(String name, String realName, String sex, int roleId, Date startDate,
			Date endDate, int state, int pageIndex) {
		// 使用map集合来存放参数
		Map<String, Object> map = new HashMap<>();
		map.put("name", name);
		map.put("realName", realName);
		map.put("sex", sex);
		map.put("roleId", roleId);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("state", state);
		int firstRow = (pageIndex - 1) * PAGE_SIZE + 1;
		int lastRow = pageIndex * PAGE_SIZE;
		map.put("firstRow", firstRow);
		map.put("lastRow", lastRow);
		
		//加载配置文件
		initSession();
		//获得mapper的代理类
		UsersDao dao = session.getMapper(UsersDao.class);
		//调用dao层方法
		List<Users> list = dao.getAllUsers(map);
		//提交事务并关闭session
		closeSession();
		return list;
	}

	@Override
	public int getTotalCount(String name, String realName, String sex, int roleId, Date startDate, Date endDate,
			int state) {
		// 使用map集合来存放参数
		Map<String, Object> map = new HashMap<>();
		map.put("name", name);
		map.put("realName", realName);
		map.put("sex", sex);
		map.put("roleId", roleId);
		map.put("startDate", startDate);
		map.put("endDate", endDate);
		map.put("state", state);
		
		//加载配置文件
		initSession();
		//获得mapper的代理类
		UsersDao dao = session.getMapper(UsersDao.class);
		// 调用dao层方法获得查询到的所有数据的个数
		int counts = dao.getTotalCount(map);
		//提交事务并关闭session
		closeSession();
		// 计算总页数
		int totalPages = counts % PAGE_SIZE == 0 ? counts / PAGE_SIZE : counts / PAGE_SIZE + 1;
		return totalPages;
	}

	@Override
	public Users getUsersById(int id) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		UsersDao dao = session.getMapper(UsersDao.class);
		//调用dao层方法
		Users user = dao.getUsersById(id);
		//提交事务并关闭session
		closeSession();
		return user;
	}

	@Override
	public boolean addUsers(Users users) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		UsersDao dao = session.getMapper(UsersDao.class);
		//调用dao层方法
		boolean flag = dao.addUsers(users);
		//提交事务并关闭session
		closeSession();
		return flag;
	}

	@Override
	public boolean deleteUsers(int id) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		UsersDao dao = session.getMapper(UsersDao.class);
		//调用dao层方法
		boolean flag = dao.deleteUsers(id);
		//提交事务并关闭session
		closeSession();
		return flag;
	}

	@Override
	public boolean updateUsers(Users users) {
		//加载配置文件
		initSession();
		//获得mapper的代理类
		UsersDao dao = session.getMapper(UsersDao.class);
		//调用dao层方法
		boolean flag = dao.updateUsers(users);
		//提交事务并关闭session
		closeSession();
		return flag;
	}

}
